#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define NotFound -1
typedef int Position;

void BuildMatch(char* pattern, int* match) {
    int m = strlen(pattern);
    match[0] = -1;
    for (int j=1; j<m; j++) {
        int i = match[j-1];
        while ((i>=0) && (pattern[i+1] != pattern[j])) {
            i = match[i];
        }
        if (pattern[i+1] == pattern[j]) {
            match[j] = i+1;
        } else {
            match[j] = -1;
        }
    }
}

Position KMP(char* string, char* pattern) {
    int n = strlen(string);
    int m = strlen(pattern);
    if (n < m) {
        return NotFound;
    }
    int *match = (int*) malloc(sizeof(int) * m);
    BuildMatch(pattern, match);
    int s = 0, p = 0;
    while (s < n && p < m) {
        if (string[s] == pattern[p]) {
            s++;
            p++;
        } else if (p > 0) {
            p = match[p-1] + 1;
        } else {
            s++;
        }
    }
    free(match);
    return (p == m) ? (s-m) : NotFound;
}

#define StringLength 1000000
#define PatternLength 100000

int main() {
    char* string = malloc(sizeof(char)*StringLength);
    char* pattern = malloc(sizeof(char)*PatternLength);

    scanf("%s", string);
    int n;
    scanf("%d", &n);
    for (int i=0; i < n; i++) {
        scanf("%s", pattern);
        Position p = KMP(string, pattern);
        if (p == NotFound) {
            printf("Not Found\n");
        } else {
            printf("%s\n", string + p);
        }
    }
    free(pattern);
    free(string);
    return 0;
}
